package com.tqfframe.task.learning.expressionModel;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: 唐祺峰
 * @Date: 2020/8/20 0020 15:46
 * @Version 1.0
 */
public class RunMain {
    public static void main(String[] args) {

        String regex="if{1}\\s*[(]{1}\\s*([\\w=!&|]*)\\s*[)]{1}\\s*[{]{1}\\s*([\\w]*)\\s*[}]{1}\\s*else{1}\\s*[{]{1}\\s*([\\w]*)\\s*[}]{1}\\s*";
        String script="if( true) {  if(true) {  ee}  else { ff} }  else {  if( true) {  gg}  else { hh}} ";
        Pattern pattern=Pattern.compile(regex);
        System.out.println(new RunMain().runMatcher(pattern,script,regex));
    }


    private String runMatcher( Pattern pattern,String script,String regex){
        String result="";
        Matcher matcher=pattern.matcher(script);
        while (matcher.find()){
            String parem0=matcher.group(0);
            String parem1=matcher.group(1);
            String parem2=matcher.group(2);
            String parem3=matcher.group(3);
//            System.out.println(matcher.group(0));
//            System.out.println(matcher.group(1));
//            System.out.println(matcher.group(2));
//            System.out.println(matcher.group(3));
            String WorkExp="";
            if(parem1.equals("true")){
                WorkExp=parem2;
            }else{
                WorkExp=parem3;
            }
            script=script.replace(parem0, WorkExp);
        }

        if(Pattern.matches(regex,script)){
            script=runMatcher(Pattern.compile(regex),script,regex);
        }
        return script;
    }


}
